<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/10 0010
 * Time: 15:36
 */

namespace app\admin\controller;

use app\admin\logic\SystemUserBll;
use service\LogService;
use think\Db;
use think\Loader;

class Login extends BasicAdmin{

    /**
     * 控制器基础方法
     */
    public function _initialize()
    {
        if (session('user') && $this->request->action() !== 'out') {
            $this->redirect('@admin');
        }
    }

    public function index(){
        if($this->request->isPost()){
            // 输入数据效验
            $data['username'] = $this->request->post('username', '', 'trim');
            $data['password'] = $this->request->post('password', '', 'trim');
            $data['verify'] = $this->request->post('verify','','trim');
            $data['__token__'] = $this->request->header('token');
            $validate = Loader::validate('User');
            if(!$validate->scene('login')->check($data)){
                $ret = doReturn("",$validate->getError(),false,[],'json',['token'=>$this->request->token()]);
            }else{
                SystemUserBll::loginSuccLogic($data['username']);
                //返回登录信息
                $ret = doReturn('登录成功，正在进入系统...','',true,[],'json',['token'=>$this->request->token()]);
            }
            $this->resultArr($ret);
        }
        return $this->fetch('', ['title' => '用户登录']);
    }

    /**
     * 退出登录
     */
    public function out()
    {
        if (session('user')) {
            LogService::write('系统管理', '用户退出系统成功');
        }
        session('user', null);
        session_destroy();
        $this->redirect('@admin/login');
        //$this->success('退出登录成功！', '@admin/login');
    }


}